package com.itheima.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.OperateLog;
import com.itheima.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 记录员工管理接口的日志信息
 */
//@Component
//@Aspect
@Slf4j
public class LogAspect2 {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private OperateLogMapper operateLogMapper;

    //定义环绕通知，记录员工管理接口的日志信息
    @Around("@annotation(com.itheima.annotation.Log)")
    public Object recordLog(ProceedingJoinPoint pjp) throws Throwable {
        //1 private Integer operateUser; //获取操作人ID
        String jwt = request.getHeader("token");
        //验证jwt，得到载荷中的id
        Claims claims = JwtUtils.parseJWT(jwt);
        Integer operateUser = (Integer) claims.get("id");

        //2 private LocalDateTime operateTime; //获取操作时间
        LocalDateTime operateTime=LocalDateTime.now();
        //3 private String className; //获取操作类名
        String className = pjp.getTarget().getClass().getName();
        //4 private String methodName; //获取操作方法名
        String methodName = pjp.getSignature().getName();
        //5 private String methodParams; //获取操作方法参数
        Object[] args = pjp.getArgs();
        String methodParams = objectMapper.writeValueAsString(args);
        //6 private String returnValue; //获取操作方法返回值,执行目标对象方法
        long start = System.currentTimeMillis();  //开始时间
        Object result = pjp.proceed();  //result对象
        String returnValue = objectMapper.writeValueAsString(result);
        //7 private Long costTime; //获取操作耗时
        long end = System.currentTimeMillis();  //结束时间
        Long costTime=end-start;

        //8 将以上数据封装成OperateLog对象，调用OperateLogMapper方法添加到表中
        OperateLog operateLog=new OperateLog(null,operateUser,operateTime,className,methodName,methodParams,returnValue,costTime);

        operateLogMapper.insert(operateLog);
        return result;
    }
}
